﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using ATW.MT.BUS;
using System.Collections;

namespace ATW.MT
{
    public partial class _customerList : System.Web.UI.Page
    {
        bus_T_appuser au = new bus_T_appuser();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserInfo"] == null)
            {
                Response.Redirect("../Main/Login.aspx");
            }
            if (!IsPostBack)
            {
                BindData();
            }
        }
        private void BindData()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["code"]))
            {
                string code = Request.QueryString["code"];
                if (!CommonClass.common.filterChar(code))
                {
                    return;
                }
                DataTable dt = au.GetUnlockedCustomer();
                rpList.DataSource = dt;
                rpList.DataBind();
            }
        }
        protected void lbnSave_Click(object sender, EventArgs e)
        {
            ArrayList al = new ArrayList();
            try
            {
                foreach (Control c in rpList.Controls)
                {
                    CheckBox ckb = (CheckBox)c.FindControl("ckb");
                    if (ckb != null && ckb.Checked)
                    {
                        HiddenField hdf = (HiddenField)c.FindControl("hdfUid");
                        if (hdf != null)
                        {
                            string uid = hdf.Value;
                            string code = Request.QueryString["code"];
                            al.Add(au.AllocateCustomer(uid, code));
                        }
                    }
                }
                if (au.ExAl(al))
                {
                    Tips("分配成功！");
                    CloseWindowReferesh();
                }
                else
                {
                    Tips("分配失败！");
                    return;
                }
            }
            catch (Exception ex)
            {

            }
        }
        private void Tips(string s)
        {//提示
            StringBuilder strScript = new StringBuilder();
            strScript.Append(@"<script language=javascript>
                var divTips=new jSharp.popUpDiv('divMsg');
                document.getElementById('msgContent').innerHTML='" + s + @"';
                divTips.Show();
                var t=setTimeout(function(){
                    divTips.Close();
                    clearTimeout(t);
                },1000);
            </script>");
            this.ClientScript.RegisterStartupScript(this.GetType(), "tips", strScript.ToString());
        }
        private void CloseWindowReferesh()
        {
            StringBuilder strScript = new StringBuilder();
            strScript.Append(@"<script language=javascript>
                if(window.opener)
                {
                    window.opener.location.href=window.opener.location.href;
                    window.close();
                }
            </script>");
            this.ClientScript.RegisterStartupScript(this.GetType(), "Close", strScript.ToString());
        }
    }
}